package HelloSeries.ui.prefs.elems.flextables.view;

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import HelloSeries.ui.prefs.elems.flextables.model.FlexTableTrackerModel;

public class FlexTableTracker extends JPanel{

	private FlexTableTrackerModel model;
	private JTable table;

	public FlexTableTracker() {
		super(new GridLayout(1,0));
		String[] entete = {"Active","Nom du site","Ordre"};
		this.model = new FlexTableTrackerModel(entete);
		this.table = new JTable (model);
		setTableProperties(table);
		initColumnSizes();
        setUpOrdreColumn(table, table.getColumnModel().getColumn(2));
		JScrollPane scrollPane = new JScrollPane(table);
		this.add(scrollPane);
	}

	private void setTableProperties(JTable table) {
		table.setShowGrid(false);
		table.setRowSelectionAllowed(false);
		table.setCellSelectionEnabled(false);
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		table.setFocusable(false);
		table.setRowHeight(25);
		table.setBackground(null);
	}
	
	private void initColumnSizes() {
		FlexTableTrackerModel model = (FlexTableTrackerModel)table.getModel();
		TableColumn column = null;
		Component comp = null;
		int headerWidth = 0;
		int cellWidth = 0;
		TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();	
        if ( headerRenderer != null )
        {
            for ( int i = 0; i < model.getColumnCount(); i++ )
            {
                column = table.getColumnModel().getColumn( i );
                if ( column != null )
                {
                    comp = headerRenderer.getTableCellRendererComponent( table, column.getHeaderValue(), false, false, 0, 0 );
                    headerWidth = comp.getPreferredSize().width;
                    comp = table.getDefaultRenderer( model.getColumnClass( i ) ).getTableCellRendererComponent( table, model.getValueAt( 0, i ), false, false, 0, i );
                    cellWidth = comp.getPreferredSize().width;
                    column.setPreferredWidth( Math.max( headerWidth, cellWidth ) );
                }
            }
		}

	}
	
	public void setUpOrdreColumn(JTable table, TableColumn ordreColumn) {
		JComboBox comboBox = new JComboBox(model.getTab_priority());
		ordreColumn.setCellEditor(new DefaultCellEditor(comboBox));

		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		ordreColumn.setCellRenderer(renderer);
	}


	public FlexTableTrackerModel getModel() {
		return model;
	}

	public void setModel(FlexTableTrackerModel model) {
		this.model = model;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

}
